'''
case Type :数据库系统
case Name ：备机重做故障，无database目录权限，主机进行create表操作，备机stop，日志记录事件
Create At: 2020-6-29
owner : @peilinqian
description:1、修改备机database目录权限2、主机进行create表操作3、查看备机状态4、查看日志5、修改备机权限，重启备机6、备机数据同步成功
history：
created： 2020/06/29  created by @peilinqian
'''

import os
import unittest
import time
import _thread
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *
from testcase.utils.PgSql import PgSql


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case012.py start------------')
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.DN_NODE_NAME = macro.DN_NODE_NAME
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.rootNode = Node(node='PrimaryRoot')
        self.dbUserNode = Node(node='PrimaryDbUser')
        self.standbyrootNode = Node(node='Standby1Root')
        self.standbydbUserNode = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()

    def test_main(self):
        # 修改备机database权限
        chmodcmd = 'chmod 000 {path}/base/'.format(path=self.DB_INSTANCE_PATH)
        chmodmsg = self.standbyrootNode.sh(chmodcmd).result()
        self.log.info('*****chmod 000 path/base*****' + chmodmsg)

        # 执行sql语句
        createcmd = 'drop table if exists test_011;create table test_011(id int,name varchar(20));'
        msg = self.primary_sh.executDbSql(createcmd)
        self.log.info(msg)

        # 查看备机状态stopped
        db_status = self.primary_sh.getDbClusterStatus('status')
        self.assertFalse(db_status)

        # 查看日志关键信息
        findfile_cmd = 'ls -t ' + self.PG_LOG_PATH + '/' + self.DN_NODE_NAME.split('/')[1] + '|head -n1'
        self.log.info(findfile_cmd)
        msg = self.standbydbUserNode.sh(findfile_cmd).result()
        self.log.info(msg)

        catfile_cmd = 'cat ' + self.PG_LOG_PATH + '/' + self.DN_NODE_NAME.split('/')[1] + '/' + msg
        self.log.info(catfile_cmd)
        msg = self.standbydbUserNode.sh(catfile_cmd).result()
        self.assertIn(self.Constant.PERMISSION_DENY_MSG, msg)
        self.log.info('logger correct')

        # 修改备机database权限
        chmodcmd = 'chmod 700 {path}/base/'.format(path=self.DB_INSTANCE_PATH)
        chmodmsg = self.standbyrootNode.sh(chmodcmd).result()
        self.log.info('*****chmod 700 path/base*****' + chmodmsg)

        # 重启备机
        msg = self.primary_sh.startDbCluster()
        self.log.info(msg)
        self.assertTrue(msg)

        time.sleep(3)

        # 执行sql语句，查看备机同步成功
        selectcmd = 'select * from test_011;'
        self.log.info(selectcmd)
        msg = self.primary_sh.executDbSql(selectcmd)
        self.log.info(msg)

    def tearDown(self):
        # 如果执行用例过程异常，tearDown时，修改base权限正常
        chmodcmd = 'chmod 700 {path}/base/'.format(path=self.DB_INSTANCE_PATH)
        self.log.info(chmodcmd)
        chmodmsg = self.standbyrootNode.sh(chmodcmd).result()
        self.log.info(chmodmsg)

        # 执行sql语句，查看备机同步成功
        dropcmd = 'drop table test_011;'
        self.log.info(dropcmd)
        msg = self.primary_sh.executDbSql(dropcmd)
        self.log.info(msg)
